package com.sniper.util;


import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author suzhen
 */
public class DateUtil {

    /**
     * 吧字符串时间转成时间戳
     *
     * @param dateStr
     * @param dateFormat
     * @return
     */
    public static long convertDateToMills(String dateStr, String dateFormat) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Date date = simpleDateFormat.parse(dateStr);
        return date.getTime();
    }


    /**
     * 获取之后几天的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
        return calendar.getTime();
    }

    public static String utcFormat(Date date) {
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd^hh:mm:ss");
        format.setTimeZone(timeZone);
        return format.format(date).replace("^", "T") + "Z";
    }

    /**
     * 时间戳转时间
     *
     * @param mills  时间戳 如果不是long 请强转一次如果不带有毫秒请乘以1000
     * @param zoneId
     * @return
     */
    public static LocalDateTime getLocalDateTime(long mills, ZoneId zoneId) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), zoneId);
    }

    /**
     * 格式化日期
     *
     * @param dateStr
     * @param dateTimeFormatter
     * @return
     */
    public static LocalDate getLocalDate(String dateStr, DateTimeFormatter dateTimeFormatter) {
        return LocalDate.parse(dateStr, dateTimeFormatter);
    }

    /**
     * 获取时间戳
     *
     * @param localDateTime
     * @return
     */
    public static long getMills(LocalDateTime localDateTime) {
        return Timestamp.valueOf(localDateTime).getTime();
    }

    /**
     * 获取日期的时间戳，开始时间在凌晨
     *
     * @param localDate
     * @return
     */
    public static long getMills(LocalDate localDate) {
        return Timestamp.valueOf(localDate.atStartOfDay()).getTime();
    }

    public static Map<String, Long> mapLongStartEndByDate(String date, int days) {

        Map<String, Long> map = new HashMap<>(3);

        //要把时间字符换转成时间戳
        LocalDateTime dateStart;
        LocalDateTime dateEnd = LocalDateTime.now();
        long start = 0;
        if (days != 0) {
            dateStart = LocalDateTime.now().plusDays(days);
            start = DateUtil.getMills(dateStart);
        }
        long end = DateUtil.getMills(dateEnd);

        if (StringUtils.isNotBlank(date)) {
            String[] d = date.split(" - ");
            dateStart = DateUtil.getLocalDate(d[0].trim(), DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();
            start = DateUtil.getMills(dateStart);
            dateEnd = DateUtil.getLocalDate(d[1].trim(), DateTimeFormatter.ISO_LOCAL_DATE).atTime(LocalTime.MAX);
            end = DateUtil.getMills(dateEnd);
        }

        map.put("start", start);
        map.put("end", end);
        map.put("offset", end - start);
        return map;
    }

    /**
     * 分割日期字符串获取最大时间和最小时间
     *
     * @param date
     * @param days 间隔天数
     * @return
     */
    public static Map<String, Integer> mapIntStartEndByDate(String date, int days) {

        Map<String, Long> mapLong = mapLongStartEndByDate(date, days);
        Map<String, Integer> mapInt = new HashMap<>(2);

        mapInt.put("start", Long.valueOf(mapLong.get("start") / 1000).intValue());
        mapInt.put("end", Long.valueOf(mapLong.get("end") / 1000).intValue());
        mapInt.put("offset", Long.valueOf(mapLong.get("offset") / 1000).intValue());
        return mapInt;
    }

    /**
     * 获取时间范围
     *
     * @param date
     * @return
     */
    public static Map<String, LocalDate> mapStartEndByDate(String date, int days) {

        Map<String, LocalDate> map = new HashMap<>(3);
        //要把时间字符换转成时间戳
        LocalDateTime dateStart = LocalDateTime.parse("1988-01-01T00:00:00");
        if (days != 0) {
            dateStart = LocalDateTime.now().plusDays(days);
        }
        LocalDateTime dateEnd = LocalDate.now().atStartOfDay();

        if (StringUtils.isNotBlank(date)) {
            String[] d = date.split(" - ");
            dateStart = DateUtil.getLocalDate(d[0].trim(), DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();
            dateEnd = DateUtil.getLocalDate(d[1].trim(), DateTimeFormatter.ISO_LOCAL_DATE).atTime(LocalTime.MAX);
        }
        map.put("start", dateStart.toLocalDate());
        map.put("end", dateEnd.toLocalDate());
        return map;
    }

    /**
     * @param localDate
     * @return
     */
    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }


}
